<?php
/**
 * Created by PhpStorm.
 * User: zhangpeng
 * Date: 2017-02-22
 * Time: 上午 10:19
 * Versions: v1.00
 */

namespace app\common\model;


class Tenpay extends Pay
{
//--------------------------------------------查询-------------------------------------------
    protected function refer($order_id){
        vendor('TenPay.WxPay#Api');
        $input = new \WxPayOrderQuery();
        $input->SetOut_trade_no($order_id);
        $result = \WxPayApi::orderQuery($input);
        if(isset($result['trade_state']) && $result['trade_state'] == 'SUCCESS'){
            $result = array('state'=>1,'errormsg'=>'pay success','data'=>$result);
            // D('order')->payOrder($order_id);
        }
        if(isset($result['err_code_des']) && $result['err_code_des']){
            $result = array('state'=>0,'errormsg'=>"wx:".$result['err_code_des'],'errorcode'=>"wx:".$result['err_code']);
            return $result;
        }
        /*else{
            $result = array('state'=>0,'errormsg'=>"wx:".$result['trade_state'],'errorcode'=>"wx:".$result['err_code']);
        }*/
        $result['data']['order_id'] = $order_id;
        return $result;
    }
    //--------------------------------------------被动接受-------------------------------------------
    protected function passive(){
        vendor('TenPay.WxPay#Config');
        vendor('TenPay.WxPay#Api');
        vendor('TenPay.WxPay#Notify');
        vendor('TenPay.log');
        $logHandler= new \CLogFileHandler("../runtime/log/".date('Y-m-d').'.log');
        $log = \Log::Init($logHandler, 15);
        $notify = new \PayNotifyCallBack();
        $notify->Handle(false);
        $data = $notify->getDataPost();
        return $data;
    }
    //--------------------------------------------支付请求 刷卡--------------------------------------------
    protected function micro($payData){
        //商品(订单)名称 价格 订单号
        $body = $payData['body'];
        $total_fee = $payData['total_fee'];
        $order_id = $payData['order_id'];
        $auth_code = $payData['auth_code'];
        //调用微信支付接口
        vendor('TenPay.WxPay#MicroPay');
        $input = new \WxPayMicroPay();
        $input ->getConfig();
        $input->SetAuth_code($auth_code);
        $input->SetBody($body);
        $input->SetTotal_fee($total_fee);
        $input->SetOut_trade_no($order_id);
        $microPay = new \MicroPay();
        $result = $microPay->pay($input);
        return $result;
    }
    //--------------------------------------------支付请求 app js--------------------------------------------
    protected function jsapi($payData){
        //商品(订单)名称 价格 订单号
        $body = $payData['body'];
        $total_fee = $payData['total_fee'];
        $order_id = $payData['order_id'];
        $openId = $payData['openid'];
        vendor('TenPay.WxPay#JsApiPay');
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($body);

        $input->SetOut_trade_no($order_id);
        $input->SetTotal_fee($total_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));

        $input->SetNotify_url(\WxPayConfig::getNotifyUrl());
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = \WxPayApi::unifiedOrder($input);

        $tools = new \JsApiPay();
        $result = $tools->GetJsApiParameters($order);
        //回调改变状态
        //model('order')->payOrder($order_id);
        return $result;
    }
    //--------------------------------------------支付请求 二维码--------------------------------------------
    protected function native($payData){
        //商品(订单)名称 价格 订单号
        $body = $payData['body'];
        $total_fee = $payData['total_fee'];
        $order_id = $payData['order_id'];

        vendor('TenPay.WxPay#NativePay');
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($body);

        $input->SetOut_trade_no($order_id);
        $input->SetTotal_fee($total_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));


        $input->SetNotify_url(\WxPayConfig::getNotifyUrl());
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id("-");

        $notify = new \NativePay();
        $result = $notify->GetPayUrl($input);
        if(isset($result["code_url"]) && $result["code_url"]){
            $ret = array('state'=>1,'errormsg'=>'ok','data'=>$result);
        }else{
            $ret = array('state'=>0,'errormsg'=>$result['err_code_des'],'data'=>$result);
        }

        return $ret;
    }
    //--------------------------------------------退款--------------------------------------------
    protected function rebate($refundData){
        $order_id 			= $refundData['order_id'];
        $total_fee 			= $refundData['pay_total_fee'];
        $refund_fee 		= $refundData['refund_fee'];
        $refund_total_fee 	= $refundData['refund_total_fee'];
        $out_trade_no = $order_id;
        vendor('TenPay.WxPay#Api');

        //refund query 退款查询 result_query
        $input = new \WxPayRefund();
        $input->SetOut_trade_no($out_trade_no);
        $result_query = \WxPayApi::refundquery($input);

        if($result_query['result_code'] == "SUCCESS"){
            //已退款金额
            $refund_fee_query = $result_query['refund_fee'];
            //数据库未存金额
            $refund_unsave = $refund_fee_query - $refund_total_fee;
            if( $refund_fee <= $refund_unsave ) return array('state'=>1,'refund'=>$refund_unsave);
            $refund_fee_continue = $refund_fee - $refund_unsave;
        }
        $refund_fee_input = isset($refund_fee_continue)?$refund_fee_continue:$refund_fee;
        //退款执行 result
        $input = new \WxPayRefund();
        $input->SetOut_trade_no($out_trade_no);
        $input->SetTotal_fee($total_fee);
        $input->SetRefund_fee($refund_fee_input);
        $input->SetOut_refund_no(\WxPayConfig::getMchid().date("YmdHis"));
        $input->SetOp_user_id(\WxPayConfig::getMchid());

        $result = \WxPayApi::refund($input);

        if(!isset($result['result_code']) || $result['result_code'] != "SUCCESS"){
            // model('Action')->add('refund tenpay',$order_id,'fail',$refund_fee);
            $errormsg = isset($result['err_code'])?$result['err_code']:$result['return_msg'];
            $ret = array('state'=>0,'errormsg'=>$errormsg,'data'=>$result);
            if(isset($refund_unsave) && $refund_unsave>0) $ret['refund'] = $refund_unsave;
            return $ret;
        }
        return array('state'=>1,'data'=>$result,'refund'=>$refund_fee);
    }
    //对账单下载
    public function downloadBill($bill_date){
        vendor('TenPay.WxPay#Api');
        $bill_type = "ALL";
        $input = new \WxPayDownloadBill();
        $input->SetBill_date($bill_date);
        $input->SetBill_type($bill_type);
        $file = \WxPayApi::downloadBill($input);
        return $file;
        //TODO 对账单文件处理
    }
}